<template>
  <el-card>
    <div class="table">
      <!-- 表格 -->
      <el-table v-loading="loading" :data="modifyRequestList" style="width: 100%">
        <el-table-column v-for="item in modifyRequestTableLabel" :key="item.prop" :label="item.label" :prop="item.prop"
          :width="item.width" :sortable="item.sortable" />
        <!-- 商品类别 -->
        <el-table-column label="商品类别">
          <template v-slot="scope">
            <el-tag v-if="currentCategoryList.length > scope.$index" type="info">
              {{ currentCategoryList[scope.$index].categoryName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column :filters="[{ text: '申请中', value: 0 }, { text: '已通过', value: 1 }, { text: '不通过', value: 2 }]"
          prop="status" label="申请状态" width="95" :filter-method="filterTag">
          <template v-slot="scope">
            <el-tag size="small" v-if="scope.row.status === 0" type="warning">申请中</el-tag>
            <el-tag size="small" v-else-if="scope.row.status === 1" type="success">已通过</el-tag>
            <el-tag size="small" v-else type="danger">不通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template v-slot="scope">
            <el-button type="primary" size="small" @click="getModifyRequestDetail(scope.$index)">查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination background layout="prev, pager, next" :total="config.total" class="pager mt-4"
        @current-change="changePage" :page-size="config.size" />
    </div>
  </el-card>

  <!-- 查看申请信息的模态框 -->
  <el-dialog v-model="dialogVisible" title="商品修改申请详情" width="50%">

    <!-- 商品详细信息展示表格 -->
    <el-descriptions :column="1" size="default" border>
      <el-descriptions-item label="修改申请ID" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.commodityModifyRequestId }}
      </el-descriptions-item>
      <el-descriptions-item label="商店ID" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.storeId }}
      </el-descriptions-item>
      <el-descriptions-item label="商品ID" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.commodityId }}
      </el-descriptions-item>
      <el-descriptions-item min-width="130px" label="商品名称" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.commodityName }}
      </el-descriptions-item>
      <el-descriptions-item label="商品价格" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.price.toFixed(2) }}
      </el-descriptions-item>
      <el-descriptions-item label="商品类别" label-align="center" align="center">
        <el-tag type="info" size="large">{{ currentCategoryList[detailIndex].categoryName }}</el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="商品库存" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.stocks }}
      </el-descriptions-item>
      <el-descriptions-item label="商品图片" label-align="center" align="center">
        <el-image v-for="item in modifyRequestDetail.modifyRequest.pictures.map(img_url_pre)" @dragstart.prevent
          style="width: 100px; height: 100px; margin-right: 10px; border-radius: 5px;" :src="item" :zoom-rate="1.2"
          :preview-src-list="modifyRequestDetail.modifyRequest.pictures.map(img_url_pre)" fit="cover" />
      </el-descriptions-item>
      <el-descriptions-item label="商品描述" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.description }}
      </el-descriptions-item>
      <el-descriptions-item label="申请发起时间" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.initiateTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请审核时间" label-align="center" align="center">
        {{ modifyRequestDetail.modifyRequest.processTime }}
      </el-descriptions-item>
      <el-descriptions-item label="申请状态" label-align="center" align="center">
        <el-select v-model="modifyRequestDetail.modifyRequest.status" placeholder="Select">
          <el-option label="申请中" :value="0" :disabled="true" />
          <el-option label="通过" :value="1" />
          <el-option label="不通过" :value="2" />
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="审核意见" label-align="center">
        <el-input v-model="modifyRequestDetail.modifyRequest.info" type="textarea" placeholder="请输入审核意见" />
      </el-descriptions-item>
    </el-descriptions>

    <!-- 底部的按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">
          取消
        </el-button>
        <el-button :disabled="originStatus !== 0" type="primary" @click="handleProcessRequest()">
          保存审核
        </el-button>
      </span>
    </template>

  </el-dialog>
</template>

<script setup>
import { getCurrentInstance, onMounted, ref, reactive } from 'vue'
import img_url_pre from '../../../utils/img_url_filter';

const { proxy } = getCurrentInstance();

let modifyRequestList = ref([]);
// 控制表格加载遮罩窗是否出现
let loading = ref(false);
// 和当前modifyRequestList中的条目相对应
let currentCategoryList = ref([]);
// 所有商品类别列表
const categoryList = ref([]);
// 当前查看详情的商品索引
let detailIndex = ref(0);

const modifyRequestTableLabel = reactive([
  {
    prop: "commodityModifyRequestId",
    label: "修改申请ID",
    width: 180,
    sortable: true,
  },
  {
    prop: "storeId",
    label: "商店ID",
    width: 180,
    sortable: true,
  },
  {
    prop: "commodityId",
    label: "商品ID",
    width: 180,
    sortable: true,
  },
  {
    prop: "commodityName",
    label: "商品名称",
    width: 180,
  },
  {
    prop: "initiateTime",
    label: "发起时间",
    width: 180,
    sortable: true,
  },
  {
    prop: "processTime",
    label: "审核时间",
    width: 180,
    sortable: true,
  },
]);

let config = reactive({
  total: 0,  // 总的条目数
  current: 1,  // 当前页
  size: 15  // 一页的条目数
})

// 获取所有商品列表
const getModifyRequestList = async () => {
  // 发起请求
  let res = await proxy.$api.commodity.getModifyRequest({
    current: config.current,
    size: config.size,
  });
  // 更新表格数据
  modifyRequestList.value = res.data.modifyRequestList;
  config.total = res.data.total;
  // 遍历并获取商品对应类别名
  currentCategoryList.value = [];
  for (let i = 0; i < modifyRequestList.value.length; ++i) {
    let ret = await proxy.$api.category.getCategory(modifyRequestList.value[i].categoryId);
    currentCategoryList.value.push(ret.data);
  }
};

// 页码改变时的动作
const changePage = async (page) => {
  loading.value = true;
  // 更新当前页码
  config.current = page
  await getModifyRequestList();
  loading.value = false;
}

let filterTag = (value, row) => {
  return row.status === value;
}

onMounted(async () => {
  // 开启加载遮罩
  loading.value = true;
  await getModifyRequestList();
  // await getAllCategory();
  // 关闭加载遮罩
  loading.value = false;
});

// 控制模态框的显示和隐藏
let dialogVisible = ref(false)

const handleProcessRequest = async () => {
  let ret = await ElMessageBox.confirm(
    '确认后将无法更改，确定保存该申请审核吗？',
    '确认审核',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    });
  if (ret === 'confirm') {
    await proxy.$api.commodity.processModifyRequest(modifyRequestDetail.modifyRequest).catch(error => {
      console.log(error);
      dialogVisible.value = false;
    })
    dialogVisible.value = false;
    // 开启加载遮罩
    loading.value = true;
    // 重新获取一下页面
    await getModifyRequestList();
    // 关闭加载遮罩
    loading.value = false;
    ElMessage.success({
      showClose: true,
      message: '审核成功',
    })
  }
}

// 开店申请表单信息
let modifyRequestDetail = reactive({
  // 再定义一层属性名，避免赋值失败
  modifyRequest: {
    "commodityModifyRequestId": "",
    "initiateTime": "",
    "processTime": "",
    "storeId": "",
    "commodityName": "",
    "categoryId": "",
    "price": 0,
    "pictures": [],
    "description": "",
    "stocks": 0,
    "status": 0,
    "info": ""
  }
});

// 当前查看详情对应索引号
let currentIndex = ref(0);
// 原本的申请状态
let originStatus = ref(0);
// 直接从modifyRequestList中根据index获取
const getModifyRequestDetail = async (index) => {
  // 更新详情对应索引
  currentIndex.value = index;
  // 更新原初状态
  originStatus.value = modifyRequestList.value[index].status;
  modifyRequestDetail.modifyRequest = modifyRequestList.value[index];
  dialogVisible.value = true
};

</script>

<style lang="less" scoped>
.table {
  position: relative;
  // height: 520px;

  .pager {
    position: relative;
    bottom: -15px;
    justify-content: flex-end;
    margin-right: 15px;
    margin-top: 10px;
    margin-bottom: 15px;
  }
}
</style>